package com.j1903.controller;

import com.aliyun.tea.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.j1903.annotation.PassJwt;
import com.j1903.util.JsonResult;
import com.j1903.service.UsersService;
import com.j1903.bean.Users;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j1903.util.JwtUtil;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author fyq
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;
    @RequestMapping("/front/reg")
    public JsonResult reg(String email,
                          String code,
                          HttpServletRequest request){
        Map<String,Object> p=
                (Map<String, Object>) request.getAttribute("tokenParam");
        if(p.get("code")!=null){
            if(p.get("code").toString().equals(code)){
                return new JsonResult(200, "验证通过", null);
            }
            return new JsonResult(500, "验证码错误", null);
        }
        else{
            return new JsonResult(500, "未获取验证码", null);
        }

    }
    @RequestMapping("/front/login")
    @PassJwt
    public JsonResult login(
            @RequestBody Map<String,String> param){
        String userName=param.get("userName");
        String userPwd=param.get("userPwd");
        List<Users> list=
                usersService.lists(new Users(userName, userPwd));
        if(list!=null&&list.size()==1){
            return new JsonResult(200, "登录成功",
                    JwtUtil.createToken(list.get(0).getId().toString(),24*60,
                            new HashMap<String,Object>(){{
                                put("userName", userName);
                                put("id", list.get(0).getId());}}));
        }else{
            return new JsonResult(500, "账号或密码错误",null);
        }

    }
    @RequestMapping("/add")
    public JsonResult add(@RequestBody Users obj){
        usersService.save(obj);
        return new JsonResult(200,"添加成功",
                obj.getId());
    }
    @RequestMapping("/upd")
    public JsonResult upd(@RequestBody Users obj){
        Boolean rs=usersService.saveOrUpdate(obj);
        if(rs){
            return new JsonResult(200,"更新成功");
        }
        else{
            return new JsonResult(500,"更新失败");
        }
    }
    @RequestMapping("/delById")
    public JsonResult delById(Long id){
        Boolean rs=usersService.removeById(id);
        if(rs){
            return new JsonResult(200,"删除成功");
        }
        else{
            return new JsonResult(500,"删除失败");
        }
    }
    @RequestMapping("/list")
    public JsonResult list(@RequestBody Users obj){
        List<Users> list=usersService.lists(obj);
        return new JsonResult(200,"查询成功",list);
    }
    @RequestMapping("/page/{curPage}/{pageRecords}")
    public JsonResult page(@RequestBody Users obj,
                           @PathVariable int curPage,
                           @PathVariable int pageRecords){
        IPage<Users> ipage=new Page<>(curPage,pageRecords);
        IPage<Users> page=usersService.pages(ipage,obj);
        return new JsonResult(200,"查询分页成功",page);
    }
}

